package com.zhfi.dataapply.schedule.writer;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;

import java.util.List;

@Slf4j
public class GeneralWriter<T> implements ItemWriter<T> {

    private BaseMapper<T> baseMapper;

    public GeneralWriter(BaseMapper<T> baseMapper){
        this.baseMapper = baseMapper;
    }

    @Override
    public void write(List<? extends T> items) throws Exception {
        for (T po : items) {
            try {
                if (baseMapper.updateById(po) <= 0) {
                    baseMapper.insert(po);
                }
            } catch (Exception e) {
                log.error("数据插入失败：" + po, e);
            }
        }
    }
}
